﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MediaWiki.Office.Packaging
{
  public class PackagePart : Container, IPackagePart
  {
    private readonly Uri _uri;
    private readonly IPackage _package;
    private readonly string _contentType;
    private readonly PartType _partType;

    /// <summary>
    /// Gets the part's type.
    /// </summary>
    /// <value>The type of the part.</value>
    public PartType PartType
    {
      get { return _partType; }
    }

    /// <summary>
    /// Gets the part's package.
    /// </summary>
    /// <value>The package.</value>
    public IPackage Package
    {
      get { return _package; }
    }

    /// <summary>
    /// Gets the container URI.
    /// </summary>
    /// <value>The URI.</value>
    public override Uri Uri
    {
      get { return _uri; }
    }

    /// <summary>
    /// Gets the part's content type.
    /// </summary>
    /// <value>The type of the content.</value>
    public string ContentType
    {
      get { return _contentType; }
    }

    public PackagePart(Uri partUri, PartType partType, IPackage package)
    {
      if (partUri == null)
        throw new ArgumentException("partUri");
      if (package == null)
        throw new ArgumentNullException("package");

      _uri = partUri;
      _package = package;
      _contentType = _package.ContentTypeHelper.GetContentType(_uri);
      _partType = partType;

      Uri relationshipUri = PackageUriHelper.GetPartRelationshipUri(_uri);

      if (_package.ContentExists(relationshipUri))
      {
        Relationships = Relationships = new PackageRelationshipCollection(_package, this);

        foreach (PackageRelationship relationship in Relationships.Where(r => r.TargetMode == TargetMode.Internal))
        {
          Uri key = PackageUriHelper.ResolvePartUri(_uri, relationship.TargetUri);

          IPackagePart part = CreatePart(key, relationship.RelationshipType);

          Parts.Add(relationship.Id, part);
        }
      }
    }

    public override IPackagePart CreatePart(Uri partUri, string relationshipType)
    {
      return new PackagePart(partUri, PackagePart.GetPartType(relationshipType), _package);
    }

    /// <summary>
    /// Gets the part's content.
    /// </summary>
    /// <returns></returns>
    public string GetContent()
    {
      return _package.ContentGetter.GetStringContent(PackageUriHelper.GetDownloaderPartName(_uri));
    }

    /// <summary>
    /// Gets the type of the part.
    /// </summary>
    /// <param name="relationshipType">Type of the relationship.</param>
    /// <returns></returns>
    public static PartType GetPartType(string relationshipType)
    {
      switch (relationshipType)
      {
        case null:
          throw new ArgumentNullException("relationshipType");

        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml":
          return PartType.CustomXml;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument":
          return PartType.GlossaryDocument;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme":
          return PartType.Theme;
        case "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail":
          return PartType.Thumbnail;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments":
          return PartType.Comments;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings":
          return PartType.DocumentSettings;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes":
          return PartType.Endnotes;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable":
          return PartType.FontTable;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes":
          return PartType.Footnotes;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering":
          return PartType.NumberingDefinitions;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles":
          return PartType.StyleDefinitions;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings":
          return PartType.WebSettings;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer":
          return PartType.Footer;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header":
          return PartType.Header;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/aFChunk":
          return PartType.AlternativeFormatImport;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image":
          return PartType.Image;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/font":
          return PartType.Font;

        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument":
          return PartType.MainDocument;
        case "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties":
          return PartType.CoreFileProperties;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties":
          return PartType.ExtendedFileProperties;
        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties":
          return PartType.CustomFileProperties;
        case "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin":
          return PartType.DigitalSignatureOrigin;

        case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps":
          return PartType.CustomXmlProperties;

        default:
          return PartType.Unknown;
      }
    }
  }
}